package com.zhiwen.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.TimeUnit;

/**
 * 缓存配置类
 */
@Configuration
@EnableCaching
public class CacheConfig {

    /**
     * 配置Caffeine缓存管理器（主要缓存管理器）
     */
    @Bean("caffeineCacheManager")
    @Primary
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .initialCapacity(100)
                .maximumSize(1000)
                .expireAfterAccess(30, TimeUnit.MINUTES)
                .recordStats());

        cacheManager.setCacheNames(java.util.Arrays.asList(
                "searchQuestions",
                "searchUsers",
                "hotKeywords",
                "searchTags",
                "searchAll",
                "aiSearch",
                "platformStatistics",
                "dashboardData",
                "hotTopics",
                "sensitiveWords",
                "sensitiveContentDetection",
                "autoReviewConfig",
                "autoReviewResult"
        ));

        return cacheManager;
    }

    /**
     * 配置短期缓存管理器（用于搜索建议等频繁变化的数据）
     */
    @Bean("shortTermCacheManager")
    public CacheManager shortTermCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .initialCapacity(50)
                .maximumSize(500)
                .expireAfterWrite(5, TimeUnit.MINUTES)
                .recordStats());

        cacheManager.setCacheNames(java.util.Arrays.asList(
                "searchSuggestions",
                "userSearchHistory"
        ));

        return cacheManager;
    }
}
